package com.yuan.fruitweb.web;

import com.yuan.fruitweb.model.Fruit;
import com.yuan.fruitweb.service.FruitService;
import com.yuan.fruitweb.service.impl.FruitServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author YuanYuemeng
 * @date 2020/11/20
 * 说明：通过地址调用
 */
//@WebServlet("/fruitList")
public class FruitListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.拿水果的数据，交给网页显示
        ApplicationContext applicationContext = (ApplicationContext) getServletContext().getAttribute("applicationContext");
        FruitService fruitService = applicationContext.getBean("fruitService",FruitService.class);

        List<Fruit> fruits = fruitService.findAllFruits();
        req.setAttribute("fruits",fruits);

        //让jsp去显示数据
        req.getRequestDispatcher("fruitList.jsp").forward(req,resp);

    }
}
